package com.edfu.core.contentmgt.jcr.service.impl.jackrabbit;

import java.util.HashMap;

import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.TransientRepository;

import com.edfu.core.contentmgt.jcr.service.impl.IJCRFactory;


public class JCRFactoryImpl implements IJCRFactory {

	public static String REPOSITORY_ROOT = null;
	public static String CREDENTIALS_USERNAME = "admin";
	public static char[] CREDENTIALS_PASSWORD = "admin".toCharArray();
//	public static String WEBDAV_SERVICE_URL = "http://localhost:8080/ContentRepository/repository/default/";
	public static String WEBDAV_SERVICE_URL = "http://localhost:8080/jackrabbit/server";


	public JCRFactoryImpl() {

	}
	
	@Override
	public Session createWebDavSession(String workspaceName)
			throws RepositoryException {
		_loadProperties();
		String url = WEBDAV_SERVICE_URL;
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(JcrUtils.REPOSITORY_URI, url);
		Repository repository = JcrUtils.getRepository(url);
		SimpleCredentials creds = new SimpleCredentials(CREDENTIALS_USERNAME,
				CREDENTIALS_PASSWORD);
		Session session = repository.login(creds, workspaceName);
		return session;
	}

	@Override
	public Session createRMISession(String workspaceName)
			throws RepositoryException {
		String url = "http://localhost:8080/ServiceRepository/rmi";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(JcrUtils.REPOSITORY_URI, url);
		Repository repository = JcrUtils.getRepository(url);
		SimpleCredentials creds = new SimpleCredentials(CREDENTIALS_USERNAME,
				CREDENTIALS_PASSWORD);
		Session session = repository.login(creds, workspaceName);
		return session;
	}

	@Override
	public Session createWebDavSession(String username,
			String password, String workspaceName) throws RepositoryException {
		_loadProperties();
		String url = WEBDAV_SERVICE_URL;
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(JcrUtils.REPOSITORY_URI, url);
		Repository repository = JcrUtils.getRepository(url);
		SimpleCredentials creds = new SimpleCredentials(username,
				password.toCharArray());
		Session session = repository.login(creds, workspaceName);
		return session;

	}

	@Override
	public Session createTransientSession(String workspaceName)
			throws RepositoryException {
		TransientRepository repo = new TransientRepository(
				"C:\\edurepo\\repository.xml",
				"C:\\edurepo\\data");
		SimpleCredentials creds = new SimpleCredentials(CREDENTIALS_USERNAME,
				CREDENTIALS_PASSWORD);
		Session session = repo.login(creds, workspaceName);
		return session;
	}

	@Override
	public Session createRMISession(String username, String password,
			String workspaceName) throws RepositoryException {
		String url = "http://localhost:8080/ServiceRepository/rmi";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(JcrUtils.REPOSITORY_URI, url);
		Repository repository = JcrUtils.getRepository(url);
		SimpleCredentials creds = new SimpleCredentials(username,
				password.toCharArray());
		Session session = repository.login(creds, workspaceName);
		return session;

	}

	// TODO Approach needs to be validated
	private static TransientRepository transientRepo;

	@Override
	public Session createTransientSession(String username, String password,
			String workspaceName) throws RepositoryException {
		if (transientRepo == null)
			transientRepo = new TransientRepository(
					"C:\\edurepo\\repository.xml",
					"C:\\edurepo\\data");
		SimpleCredentials creds = new SimpleCredentials(username,
				password.toCharArray());
		Session session = transientRepo.login(creds, workspaceName);
		return session;

	}

	private static void _loadProperties() {
	
	}

}
